﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AP.Framework.Infrastructure.Entities;
using AP.Framework.Infrastructure.Repositories.IRepositories;
using AP.Framework.Infrastructure.Database;
using System.Data;

namespace AP.Framework.Infrastructure.Repositories
{
    public class ModulesRepository : Repository<sys_Modules>, IModules
    {
        protected override Func<sys_Modules, object> DefaultOrderBy
        {
            get
            {
                return x => x.ID;
            }
        }

        public ModulesRepository(ApContext context) : base(context) { }

        public override void Add(sys_Modules entity)
        {
            //var exists = DefaultSet.Any(x => x.Login.Equals(entity.Login) || x.Email.Equals(entity.Email, StringComparison.InvariantCultureIgnoreCase));
            var exists = DefaultSet.Any();
            if (!exists)
            {
                base.Add(entity);
            }
        }

        public override sys_Modules FindById(int id)
        {
            return (from entity in DefaultSet
                    where id == entity.ID
                    select entity).SingleOrDefault();
        }

        public override IEnumerable<sys_Modules> FindById(params int[] ids)
        {
            return from entity in DefaultSet
                   where ids.Contains(entity.ID)
                   select entity;
        }
        public override void Modify(sys_Modules entity)
        {
            ApContext context = new ApContext();
            var entry = context.Entry(entity);
            if (entry.State == EntityState.Detached)
            {
                entry.State = EntityState.Modified;
            }
            context.SaveChanges();
        }
    }
}
